{
 "cells": [
  {
   "cell_type": "raw",
   "id": "a47da0d0-0927-4adb-93e6-99a434f732cf",
   "metadata": {},
   "source": [
    "---\n",
    "sidebar_position: 2\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2195672-0cab-4967-ba8a-c6544635547d",
   "metadata": {},
   "source": [
    "# Routing\n",
    "\n",
    "Sometimes we have multiple indexes for different domains, and for different questions we want to query different subsets of these indexes. For example, suppose we had one vector store index for all of the LangChain python documentation and one for all of the LangChain js documentation. Given a question about LangChain usage, we'd want to infer which language the the question was referring to and query the appropriate docs. **Query routing** is the process of classifying which index or subset of indexes a query should be performed on."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4079b57-4369-49c9-b2ad-c809b5408d7e",
   "metadata": {},
   "source": [
    "## Setup\n",
    "#### Install dependencies\n",
    "\n",
    "```{=mdx}\n",
    "import IntegrationInstallTooltip from \"@mdx_components/integration_install_tooltip.mdx\";\n",
    "import Npm2Yarn from \"@theme/Npm2Yarn\";\n",
    "\n",
    "<IntegrationInstallTooltip></IntegrationInstallTooltip>\n",
    "\n",
    "<Npm2Yarn>\n",
    "  @langchain/core @langchain/openai zod\n",
    "</Npm2Yarn>\n",
    "```\n",
    "\n",
    "#### Set environment variables\n",
    "\n",
    "We'll use OpenAI in this example:\n",
    "\n",
    "```\n",
    "OPENAI_API_KEY=your-api-key\n",
    "\n",
    "# Optional, use LangSmith for best-in-class observability\n",
    "LANGSMITH_API_KEY=your-api-key\n",
    "LANGCHAIN_TRACING_V2=true\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8b08c52-1ce9-4d8b-a779-cbe8efde51d1",
   "metadata": {},
   "source": [
    "## Routing with function calling models\n",
    "\n",
    "With function-calling models it's simple to use models for classification, which is what routing comes down to:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "783c03c3-8c72-4f88-9cf4-5829ce6745d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import { ChatPromptTemplate } from \"@langchain/core/prompts\";\n",
    "import { ChatOpenAI } from \"@langchain/openai\";\n",
    "import { z } from \"zod\";\n",
    "\n",
    "const routeQuerySchema = z.object({\n",
    "    datasource: z.union([\n",
    "        z.literal(\"python_docs\"),\n",
    "        z.literal(\"js_docs\"),\n",
    "        z.literal(\"golang_docs\"),\n",
    "    ]).describe(\"Given a user question choose which datasource would be most relevant for answering their question\")\n",
    "});\n",
    "\n",
    "const system = `You are an expert at routing a user question to the appropriate data source.\n",
    "\n",
    "Based on the programming language the question is referring to, route it to the relevant data source.`\n",
    "\n",
    "const prompt = ChatPromptTemplate.fromMessages(\n",
    "[\n",
    "    [\"system\", system],\n",
    "    [\"human\", \"{question}\"],\n",
    "]\n",
    ")\n",
    "const llm = new ChatOpenAI({\n",
    "modelName: \"gpt-3.5-turbo-0125\",\n",
    "temperature: 0\n",
    "});\n",
    "const llmWithTools = llm.withStructuredOutput(routeQuerySchema, {\n",
    "    name: \"RouteQuery\"\n",
    "})\n",
    "\n",
    "const router = prompt.pipe(llmWithTools);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "61c682f1-9c46-4d7e-b909-5cfdabf41544",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{ datasource: \u001b[32m\"python_docs\"\u001b[39m }"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "const question = `Why doesn't the following code work:\n",
    "\n",
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "\n",
    "prompt = ChatPromptTemplate.from_messages([\"human\", \"speak in {language}\"])\n",
    "prompt.invoke(\"french\")`\n",
    "await router.invoke({\"question\": question})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3be4c9de-3b79-4f78-928c-0a65a0b87193",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{ datasource: \u001b[32m\"js_docs\"\u001b[39m }"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "const question = `Why doesn't the following code work:\n",
    "\n",
    "\n",
    "import { ChatPromptTemplate } from \"@langchain/core/prompts\";\n",
    "\n",
    "\n",
    "const chatPrompt = ChatPromptTemplate.fromMessages([\n",
    "  [\"human\", \"speak in {language}\"],\n",
    "]);\n",
    "\n",
    "const formattedChatPrompt = await chatPrompt.invoke({\n",
    "  input_language: \"french\"\n",
    "});`\n",
    "\n",
    "await router.invoke({\"question\": question})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38e01995-fa14-4f55-96e9-ae17d0d86e48",
   "metadata": {},
   "source": [
    "## Routing to multiple indexes\n",
    "\n",
    "If we may want to query multiple indexes we can do that, too, by updating our schema to accept a List of data sources:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "af62af17-4f90-4dbd-a8b4-dfff51f1db95",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{ datasources: [ \u001b[32m\"python_docs\"\u001b[39m, \u001b[32m\"js_docs\"\u001b[39m ] }"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import { z } from \"zod\";\n",
    "\n",
    "const routeQuerySchema = z.object({\n",
    "    datasources: z.array(z.union([\n",
    "        z.literal(\"python_docs\"),\n",
    "        z.literal(\"js_docs\"),\n",
    "        z.literal(\"golang_docs\"),\n",
    "    ])).describe(\"Given a user question choose which datasources would be most relevant for answering their question\")\n",
    "}).describe(\"Route a user query to the most relevant datasource.\");\n",
    "\n",
    "\n",
    "const llm = new ChatOpenAI({\n",
    "    modelName: \"gpt-3.5-turbo-0125\",\n",
    "    temperature: 0\n",
    "  });\n",
    "const llmWithTools = llm.withStructuredOutput(routeQuerySchema, {\n",
    "name: \"RouteQuery\"\n",
    "})\n",
    "const router = prompt.pipe(llmWithTools);\n",
    "await router.invoke(\n",
    "    {\n",
    "        \"question\": \"is there feature parity between the Python and JS implementations of OpenAI chat models\"\n",
    "    }\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Deno",
   "language": "typescript",
   "name": "deno"
  },
  "language_info": {
   "file_extension": ".ts",
   "mimetype": "text/x.typescript",
   "name": "typescript",
   "nb_converter": "script",
   "pygments_lexer": "typescript",
   "version": "5.3.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
